package czx.wt.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import czx.wt.common.exception.BusinessException;
import czx.wt.common.response.ComResponseEnum;
import czx.wt.common.response.ResponseData;
import czx.wt.system.pojo.Role;
import czx.wt.system.pojo.RoleAuthorities;
import czx.wt.system.pojo.UserRole;
import czx.wt.system.service.IRoleAuthoritiesService;
import czx.wt.system.service.IRoleService;
import czx.wt.system.service.IUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wf.jwtp.annotation.RequiresPermissions;

import javax.validation.Valid;

/**
 * @Author: ChenZhiXiang
 * @Date: 2019/6/25 22:18
 * @Description: 角色控制层
 */
@Api(value = "角色相关接口",tags = "角色相关接口")
@RestController
@RequestMapping("${api.version}")
public class RoleController extends BaseController{

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IRoleAuthoritiesService roleAuthoritiesService;

    @ApiOperation(value = "查询所有角色",notes = "根据一些条件查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "roleName", value = "角色名",required = false,dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "comments", value = "描述", required = false,dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "query")
    })
    @RequiresPermissions("get:/v1/findAllRole")
    @GetMapping("/findAllRole")
    public Object findAllUser(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "roleName",required = false) String roleName,
                       @RequestParam(value = "comments",required = false) String comments,
                       @RequestParam(value = "access_token",required = true) String access_token) throws Exception {

        EntityWrapper<Role> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time",false);
        if (StringUtils.isNotBlank(roleName)){
            wrapper.eq("role_name",roleName);
        } else if (StringUtils.isNotBlank(comments)){
            wrapper.eq("comments",comments);
        } else if (StringUtils.isNotBlank(roleName) && StringUtils.isNotBlank(comments)){
            wrapper.eq("role_name",roleName).and().eq("comments",comments);
        }
        Page<Role> page = new Page<>(pageNum,pageSize);
        return roleService.selectPage(page,wrapper);
    }

    @ApiOperation(value = "增加角色",notes = "增加角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色信息", required = true, dataType = "role", paramType = "body"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "query")
    })
    @RequiresPermissions("post:/v1/addRole")
    @PostMapping("/addRole")
    public Object addRole(@Valid @RequestBody Role role,
                          @RequestParam(value = "access_token",required = true) String access_token) throws Exception {
        boolean isAddSuccess = roleService.insert(role);
        if (isAddSuccess){
            return ResponseData.SUCCESS;
        }
        return ResponseData.OPERATION;
    }

    @ApiOperation(value = "修改角色",notes = "修改角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色信息", required = true, dataType = "role", paramType = "body"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "query")
    })
    @RequiresPermissions("post:/v1/updateRole")
    @PostMapping("/updateRole")
    public Object updateRole(@Valid @RequestBody Role role,
                          @RequestParam(value = "access_token",required = true) String access_token) throws Exception {
        boolean isUpSuccess = roleService.updateById(role);
        if (isUpSuccess){
            return ResponseData.SUCCESS;
        }
        return ResponseData.OPERATION;
    }

    @RequiresPermissions("delete:/v1/delRoleById")
    @ApiOperation(value = "删除角色", notes = "根据角色编码删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "form")
    })
    @DeleteMapping("/delRoleById")
    public Object delRoleById(@RequestParam(value = "roleId",required = true) Integer roleId) {
        // 先删除角色表角色信息
        if (roleService.deleteById(roleId)) {
            // 接着删除用户角色信息表的角色信息与角色权限信息表信息
            EntityWrapper<UserRole> wrapper = new EntityWrapper<>();
            wrapper.eq("role_id",roleId);
            boolean isSucc1 = userRoleService.delete(wrapper);
            EntityWrapper<RoleAuthorities> wrapper2 = new EntityWrapper<>();
            wrapper2.eq("role_id",roleId);
            boolean isSucc2 = roleAuthoritiesService.delete(wrapper2);
            if (!(isSucc1 && isSucc2)){
               throw new BusinessException(ComResponseEnum.OPERATION);
            }
            return ResponseData.SUCCESS;
        }
        return ResponseData.OPERATION;
    }
}
